package com.paradogs.framework.master.netty;

import com.baidu.bjf.remoting.protobuf.annotation.ProtobufClass;
import com.paradogs.framework.common.pojo.cg.CGPrepareLogin;
import com.paradogs.framework.core.msg.ByteData;
import com.paradogs.framework.core.msg.MsgHeader;
import com.paradogs.framework.core.netty.server.ServerProperties;
import com.paradogs.framework.core.queue.AbstractMsgConsumer;
import com.paradogs.framework.core.utils.MsgUtils;
import com.paradogs.framework.core.utils.ProtoUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;

/**
 * @author: yumi
 * @date: 2023/11/6  17:27
 * @Description: TODO
 */
@Slf4j
@Primary // 不好控制 bean 注册顺序, @onMissingBean 会失败（@Scope 会导致该注解失效），使用 @Primary 覆盖默认类
@Component
public class MasterMsgConsumer extends AbstractMsgConsumer {


    @Autowired
    private ServerProperties serverProperties;

    @Override
    public void handle(ByteData byteData) throws InvocationTargetException, IllegalAccessException {

        MsgHeader header = byteData.getMsgHeader();

        /**
         * 发给自己的消息直接处理
         */
        if (header.getServer().equals(serverProperties.getType())) {
            MsgUtils.adapterHandle(byteData);
            return;
        }

        /**
         * 其他 RPC 消息通过 Master 进行转发
         */
        MsgUtils.forward(byteData);
    }
}
